<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/12/9
 * Time: 17:16
 */

include_once(ROOTDIR.'include/ap.class.php');
include_once(ROOTDIR.'include/sdk.class.php');

if(empty($_SESSION["userinfo"])){die("<script>top.location.href='index.php'</script>");}

//菜单管理
class Menu_con extends ap
{
    //菜单页面
    public function menu()
    {
        $userid = $this->userinfo['userid'];

        //当重新进入本页面的时候，判断是否发布
        $count = $this->viewdata("count(*) as num","apmenu","where userid={$userid} AND is_publish=1 ");
        $nopubnum = $count['num'];
        //print_r($nopubnum);

        $data = $this->arrdata("*","apmenu","where userid={$userid} AND is_del=1 ");

        $newdata = array();
        foreach ($data as $k => $v) {
            //图文广播
            if($v['menuType'] == 'in' || $v['menuType'] == 'page')
            {
                $param = substr($v['actionParam'], 5);
                $param = $param ? $param : 0;
                $info = $this->viewdata("*","apfodder","where msgid={$param}");
                $title = $info['title'];
                $newdata[$k]['actionParam'] = $title;
            }
            else
            {
                $newdata[$k]['actionParam'] = $v['actionParam'];
            }

            $newdata[$k]['id'] = $v['id'];
            $newdata[$k]['title'] = $v['title'];
            $newdata[$k]['menuType'] = $v['menuType'];
            $newdata[$k]['actionType'] = $v['actionType'];
            $newdata[$k]['pid'] = $v['pid'];
            $newdata[$k]['level'] = $v['level'];
            $newdata[$k]['sort'] = $v['sort'];
        }

        $menu = $this->multigroup($newdata);
        foreach ($menu as $k => $v) {
            //子菜单个数
            $menu[$k]['subnum'] = count($v['subButton']);
        }
        //主菜单个数
        $mainnum = count($menu);

        //素材列表
        $fodder = $this->arrdata('*','apfodder',"where userid={$userid}");

        $data = array();
        foreach ($fodder as $k => $v)
        {
            $data[$v['msgid']][] = array(
                'id'       => $v['id'],
                'userid'   => $userid,
                'title'    => $v['title'],
                'img_url'  => $v['img_url'],
                'abstract' => $v['abstract'],
                'content'  => $v['content'],
                'time'     => $v['time'],
                'type'     => $v['type'],
            );
        }

        //echo '<pre>';
        //print_r($menu);die;
        $this->smarty->assign("nopubnum",$nopubnum);
        $this->smarty->assign("fodder",$data);
        $this->smarty->assign("mainnum", $mainnum);
        $this->smarty->assign("menu", $menu);
        $this->smarty->display("query_menu.tpl");
    }

    //添加主菜单、子菜单名称
    public function addmenu()
    {
        $into = array(
            'title'      => $_POST['title'],
            'pid'        => $_POST['pid'],
            'level'      => $_POST['level'],
            'userid'     => $this->userinfo['userid'],
        );
        //$this->updata("apuser",$into,"where id={$ishave['id']}");
        $res = $this->setdata("apmenu",$into);
        if($res)
        {
            $id = mysql_insert_id();
            //$info = $this->viewdata("*","apmenu","where id={$id}");

            $data = array(
                'status' => 1,
                'id' => $id,
            );
        }

        echo json_encode($data);
    }

    //修改主菜单、子菜单
    public function editmenu()
    {
        $id = (int)$_POST['id'];

        $into = array(
            'title'   => $_POST['title'],
            'is_publish'   => 1,
        );

        $res = $this->updata("apmenu",$into,"where id={$id}");
        if($res)
        {
            $data = array(
                'status' => 1,
                'id' => $id,
            );
        }

        echo json_encode($data);


    }

    //删除主菜单、子菜单
    public function delmenu()
    {
        //获取要删除的记录id
        $id = (int)$_POST['id'];
        //删除该记录
        $into = array('is_del' => 2, 'is_publish' => 1);
        $this->updata("apmenu",$into,"where id={$id}");

        //判断是否为主菜单
        if($_POST['level'] == 1)
        {
            //删除子菜单
            $this->updata("apmenu",$into,"where pid={$id}");

        }

        $data = array(
            'status' => 1,
        );

        echo json_encode($data);
    }

    //添加菜单类型
    public function addtype()
    {
        //print_r($_POST);die;
        //获取要添加菜单类型的id
        $id = (int)$_POST['id'];

        $into = array(
            'menuType'   => $_POST['menuType'],
        );

        //$config = Lxbase::getconfig('ap');
        switch($into['menuType'])
        {
            case 'link':
                $into['actionType'] = 'link';
                $into['actionParam'] = $_POST['actionParam'];
                break;
            case 'in':
            case 'page':
                $into['actionType'] = 'out';
                $into['actionParam'] = 'MENU_'. $_POST['actionParam'];
                break;
            case 'map':
                $into['actionType'] = 'alipay';
                $into['actionParam'] = $_POST['actionParam'];
                break;
            case 'tel':
                $into['actionType'] = 'tel';
                $into['actionParam'] = $_POST['actionParam'];
                break;
        }

        //print_r($into);die;
        $this->updata("apmenu",$into,"where id={$id}");
        echo json_encode(array('status' => 1));

    }

    //删除主菜单效果
    public function deltype()
    {
        $id = $_POST['id'];

        $into = array(
            'menuType' => '',
            'actionType' => '',
            'actionParam' => '',
        );

        $this->updata("apmenu",$into,"where id={$id}");

        echo json_encode(array('status' => 1));die;
    }

    //发布菜单
    public function publish()
    {
        $userid = $this->userinfo['userid'];
        //删除
        $this->deldata('apmenu',"where userid={$userid} AND is_del=2");

        $data = $this->arrdata("*","apmenu","where userid={$userid}");
        $newdata = array();
        foreach ($data as $k => $v) {
            //地图
            if($v['menuType'] == 'map')
            {
                $newdata[$k]['actionParam'] = "alipays://platformapi/startapp?appId=20000050&title=%e9%99%84%e8%bf%91%e7%bd%91%e7%82%b9&keywords=". urlencode($v['actionParam']) ."&publicId=#publicId#&sourceId=publicPlatform";
            }
            else
            {
                $newdata[$k]['actionParam'] = $v['actionParam'];
            }

            $newdata[$k]['id'] = $v['id'];
            $newdata[$k]['pid'] = $v['pid'];
            $newdata[$k]['name'] = $v['title'];
            $newdata[$k]['actionType'] = $v['actionType'];

        }

        //echo '<pre>';
        //print_r($newdata);
        $menu = $this->multigroup($newdata);
        foreach ($menu as $k => $v) {
            if(!$v['subButton'])
            {
                unset($menu[$k]['subButton']);
            }
            else
            {
                unset($menu[$k]['actionParam']);
                unset($menu[$k]['actionType']);
                foreach ($v['subButton'] as $kk => $vv) {
                    if(!$vv['subButton'])
                    {
                        unset($menu[$k]['subButton'][$kk]['subButton']);
                    }
                }
            }
        }
        //print_r($menu);die;
        $config = $this->getapinfo($userid);
        $request = new sdk();

        //创建菜单
        $biz_content = array('button' => $menu);

        $biz_content = json_encode($biz_content);

        $request->setBizContent($biz_content);
        $request->setApiMethodName("alipay.mobile.public.menu.add");

        $result = $this->aopclient_request_execute($request, $config);

        if($result != null && $result->alipay_mobile_public_menu_add_response->code == 200)
        {
            $this->updata("apmenu",array('is_publish' => 2),"where userid={$userid}");

            echo json_encode(array('status' => 1));die;
            //echo $result->alipay_mobile_public_menu_add_response->code;
            //echo $result->alipay_mobile_public_menu_add_response->msg;
        }
        else{
            echo json_encode(array('status' => 0));die;
        }

    }

    //组合多维数组
    public function multigroup($cate, $child='subButton', $pid=0){
        $arr = array();
        foreach ($cate as $v) {
            if($v['pid'] == $pid){
                $v[$child] = $this->multigroup($cate, $child, $v['id']);
                $arr[] = $v;
            }
        }
        return $arr;
    }


}